package com.ruoyi.chargepile.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.chargepile.domain.ChargePile;
import com.ruoyi.chargepile.domain.ChargeRecords;
import com.ruoyi.chargepile.domain.enums.ChargePileStatusEnum;
import com.ruoyi.chargepile.domain.enums.ChargeRecordsStatusEnum;
import com.ruoyi.chargepile.mapper.ChargePileMapper;
import com.ruoyi.chargepile.mapper.ChargeRecordsMapper;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.mapper.SysJobMapper;
import com.ruoyi.quartz.service.ISysJobService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("changePileStatusTask")
public class ChangePileStatusTask
{
    @Autowired
    private ISysJobService iSysJobService;

    @Resource
    private ChargePileMapper chargePileMapper;

    @Resource
    private ChargeRecordsMapper chargeRecordsMapper;

    @Resource
    private SysJobMapper sysJobMapper;

    public void changePileStatus(Integer chargeRecordsId)
    {
        ChargeRecords chargeRecords = chargeRecordsMapper.selectById(chargeRecordsId);
        chargeRecords.setChargeStatus(ChargeRecordsStatusEnum.COMPLETED.getCode());
        chargeRecordsMapper.updateById(chargeRecords);

        ChargePile chargePile = chargePileMapper.selectById(chargeRecords.getChargePileId());
        System.out.println("++++++++++++++++++++++");
        System.out.println("充电桩状态" + chargePile.getStatus());
        System.out.println("++++++++++++++++++++++");
        chargePile.setStatus(ChargePileStatusEnum.IDLE.getCode());
        chargePileMapper.updateById(chargePile);

        SysJob sysJob = sysJobMapper.selectJobById(Long.valueOf(chargeRecords.getId()));
        try {
            iSysJobService.deleteJob(sysJob);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        System.out.println("++++++++++++++++++++++");
        System.out.println("充电桩状态" + chargePile.getStatus());
        System.out.println("充电桩Id" + chargePile.getId());
        System.out.println("充电记录id" + chargeRecords.getId());
        System.out.println("++++++++++++++++++++++");
    }
}
